package com.yqg.education.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yqg.education.common.Result;
import com.yqg.education.entity.Live;
import com.yqg.education.mapper.LiveMapper;
import com.yqg.education.service.ILiveService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * LiveController 前端控制器
 *
 * @author 张芸博
 * @since 2025-04-10
 */
@RestController
@RequestMapping("/live")
public class LiveController {

    @Resource
    private ILiveService liveService;

    @Resource
    private LiveMapper liveMapper;

    /**
     * 新增或者更新
     */
    @PostMapping("/edit")
    public Result save(@RequestBody Live live) {
        if (ObjectUtil.isEmpty(live.getId())) {
            live.setFlow(0);
        }
        return Result.success(liveService.saveOrUpdate(live));
    }

    /**
     * 删除
     */
    @GetMapping("delete/{id}")
    public Result delete(@PathVariable Integer id) {
        Live live = liveService.getById(id);
        live.setIsDelete(1);
        return Result.success(liveService.saveOrUpdate(live));
    }

    /**
     * 查询所有
     */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<Live> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        List<Live> list = liveService.list(queryWrapper);
        return Result.success(list);
    }

    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(liveService.getById(id));
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(required = false) Integer courseId,
                           @RequestParam(required = false) Integer teachId,
                           @RequestParam(required = false) Integer flow) {
        pageNum = pageNum * pageSize - pageSize;
        List<Live> liveList = liveMapper.findPage(pageNum, pageSize, courseId, teachId, flow);
        Long count = liveMapper.findCount(courseId, teachId, flow);
        HashMap<String, Object> map = new HashMap<>();
        map.put("records", liveList);
        map.put("total", count);
        return Result.success(map);
    }

    @GetMapping("/startStreaming")
    public Result startStreaming(Integer id) {
        Live live = liveService.getById(id);
        if (live.getFlow() == 1) {
            return Result.error("201", "正在直播中");
        }
        live.setStartTime(LocalDateTime.now());
        live.setFlow(1);
        return Result.success(liveService.saveOrUpdate(live));
    }

    @GetMapping("/stopStreaming")
    public Result stopStreaming(Integer id) {
        Live live = liveService.getById(id);
        live.setEndTime(LocalDateTime.now());
        live.setFlow(2);
        return Result.success(liveService.saveOrUpdate(live));
    }

    /**
     * 学员端查询课程分页查询课程直播数据
     */
    @GetMapping({"stuPage"})
    public Result findStuPage(@RequestParam Integer pageNum,
                              @RequestParam Integer pageSize,
                              String name,
                              @RequestParam(required = false) Integer typeId,
                              @RequestParam Integer userId,
                              @RequestParam(required = false) Integer courseId) {
        return Result.success(liveService.findStuPage(pageNum,pageSize,name,typeId,userId,courseId));
    }

    @GetMapping("/updateFlow")
    public Result updateFlow(Integer id,
                             Integer flow) {
        Live live = liveService.getById(id);
        if (flow == 1) {
            live.setStartTime(LocalDateTime.now());
        } else {
            live.setEndTime(LocalDateTime.now());
        }
        live.setFlow(flow);
        return Result.success(liveService.saveOrUpdate(live));
    }
}

